//
//  UITableViewController+HDCityAreaController.m
//  app
//
//  Created by D on 15/5/13.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "HDCityAreaController.h"
#import "HDCityAreaHttpResult.h"
#import "HDCityAreaDataManager.h"
#import "HDCityButton.h"


static const CGFloat kCartTotalPanelHeight = 120;

@interface HDCityAreaController ()

@property (nonatomic,strong) HDCityAreaHttpResult * cityAreaResult;
@property (nonatomic,strong) HDCityAreaDataManager * cityAreaDataManager;
@property (nonatomic,assign) CGFloat cellHight;

@end

@implementation HDCityAreaController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"选择城市";

    [self.navigationController setNavigationBarHidden:NO];
    UITableView * tableView = [[UITableView alloc]initWithFrame:CGRectOf(CGPointMake(0, 0),
                                                                         [self.view.ubo fw:UB_WIDTH fh:UB_HEIGHT uh:-kCartTotalPanelHeight oh:-(self.navigationAndStatusBarHeight+ktabBarHeight)]) style:UITableViewStyleGrouped];
    self.tableView = tableView;
  
   
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    self.cityAreaResult = [[HDCityAreaHttpResult alloc]initWithTarget:self action:@selector(cityLoadData:)];
    self.cityAreaDataManager = [HDCityAreaDataManager shareManager];
    
    if ([self.cityAreaDataManager shouldRfreshCityData]) {
        //        显示转菊花并发送请求
        [self.tableView.nothingRefreshView show];
        [self requestWithCity];
    }else
    {
        if (![self.cityAreaDataManager fmdbIncludeCity:self.pid] || ![self.cityAreaDataManager fmdbIncludeData:self.pid]){
            //        显示转菊花并发送请求
            [self.tableView.nothingRefreshView show];
            [self requestWithCity];
        }else
        {
            if ([self.cityAreaDataManager shouldRefreshCityData:self.pid]) {
                [self.tableView.nothingRefreshView show];
                [self requestWithCity];
            }else
            {
                 self.cityAreaResult.dataArray = [self.cityAreaDataManager arrayFromFmdb:self.pid];
            }
         
        }
           
    }
    
    
    
}

#pragma mark - 网络请求
-(void)requestWithCity
{
    [self.cityAreaDataManager loadCityAreaData:self.cityAreaResult WithPid:self.pid];
}

#pragma mark - 刷新网络请求
-(void)refreshWihtProvince
{
    [self requestWithCity];
}
#pragma mark - 数据处理
-(void)cityLoadData:(HDCityAreaHttpResult*)result
{
    [self.tableView endRefreshingAll];
   
    if (result.success) {
         NSLog(@"%d",result.dataArray.count);
        [self.tableView.nothingRefreshView remove];
        [self.tableView reloadData];
    }
}

#pragma mark - table view 代理
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return 1;

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    static NSString *CellIdentifier = @"CellIdentifier";
    HDAreaCell *cell = [[HDAreaCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    cell.backgroundColor = [UIColor whiteColor];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.delegate = self;
    [cell cellWithArray:self.cityAreaResult.dataArray];
    
    cell.backgroundView = nil;
    
    return cell;

}
#pragma mark -  headerView
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    
    HDCityHeaderView * headerView = [[HDCityHeaderView alloc] initWithDelegate:self ];
    headerView.cityLab.text = self.cityName;
    headerView.backgroundColor = [UIColor clearColor];
    return headerView;

}

#pragma mark - headerView 高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 56;
}

-(void)cityCellHigth:(CGFloat)cellHight
{
    self.cellHight = cellHight;

}
#pragma mark - 点击区县
-(void)selectCellButton:(HDCityButton *)sender
{
    if ([self.provinceName isKindOfClass:[NSNull class]]) {
        self.provinceName = @"";
    }
    NSString * address = [NSString stringWithFormat:@"%@,%@,%@",self.provinceName,self.cityName,sender.titleLabel.text];
    [[NSUserDefaults standardUserDefaults] setObject:address forKey:kHZMallUserAddressEditStore];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
//    返回编辑界面
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:kHZMallUserAddressIsEdit];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    NSInteger index = [[self.navigationController viewControllers] indexOfObject:self];
    if (index <= 0) {
        index = 0;
    }
    [self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:index-2] animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return  self.cellHight;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
